home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 524 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.3 KB  |  53 lines

  1. Path: surfnet.nl!sun4nl!ittpub!ittpub!nntp
  2. Newsgroups: comp.std.c
  3. Subject: alignment requirements for all structures?
  4. Message-ID: <1996Mar7.151216.1793@ittpub>
  5. From: wil@ittpub.nl (Wil Evers)
  6. Date: 7 Mar 96 15:12:16 WET
  7. Distribution: world
  8. Nntp-Posting-Host: lintilla
  9.  
  10. Hi!
  11.  
  12. I have a few questions for the ANSI/ISO C gurus:
  13.  
  14. (1). Is there a rule in the C standard stating that the address of *every*  
  15. structure - irrespective of its contents - should satisfy some alignment  
  16. requirement? For example, is the following guaranteed to work?
  17.  
  18.     struct a { char data[sizeof(double)]; } ana;
  19.     struct b { double data; };
  20.  
  21.     int main()
  22.     {
  23.         ((struct b *) &ana)->data = 42.42;
  24.         return 0;
  25.     }
  26.  
  27. (2). If the answer to (1) is no, then is a conforming compiler allowed to  
  28. assign some specific alignment requirement to a data type, just because it  
  29. is defined as a structure, and again irrespective of the contents of the  
  30. structure? For example, if I write:
  31.     
  32.     #include <stdio.h>
  33.  
  34.     struct c { char data; } carray[10];
  35.  
  36.     int main()
  37.     {
  38.         printf("%u\n", sizeof carray);
  39.         return 0;
  40.     }
  41.  
  42. would it be legal for this program to print something other than 10?
  43.  
  44. (3). If the answer to (2) is yes, why would a compiler want to do this?
  45.  
  46. Thanks in advance,
  47.  
  48. - Wil
  49.  
  50. Wil Evers, <wil@ittpub.nl>
  51. ITT Publitec Research and Development BV, Amsterdam, Holland
  52.   
  53.